package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.FindLast;
import com.lanou.dllo.findfood.util.MyFoodItemLisenter;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class BeautufulAdpter extends RecyclerView.Adapter<BeautufulAdpter.BeRecylerAdpter> {
  private   List<FindLast> list;
    private MyFoodItemLisenter itemLisenter;

    public  void  addData( List<FindLast> lists){
        list = lists;
        notifyDataSetChanged();
    }
    public void setItemLisenter(MyFoodItemLisenter item){
        itemLisenter = item;
    }
    @Override
    public BeRecylerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyler_findb_item,null);
        return new BeRecylerAdpter(view);
    }

    @Override
    public void onBindViewHolder(BeRecylerAdpter holder, int position) {
        holder.position = position;
        FindLast last = list.get(position);
        if (last!= null){
            holder.description.setText(last.getDescription());
            holder.keyWords.setText(last.getKeywords());
            holder.name.setText(last.getName());
            String imgUrl = "http://tnfs.tngou.net/image/" + last.getImg();
            getBitmap(imgUrl,holder.img);
        }

    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size():0;
    }


    class  BeRecylerAdpter extends  RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView img;
        private TextView name,keyWords,description;
        private int position;

        private LinearLayout linearLayout;

        public BeRecylerAdpter(View itemView) {
            super(itemView);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.blitme);
            linearLayout.setOnClickListener(this);
            img = (NetworkImageView) itemView.findViewById(R.id.Bimg);
            name = (TextView) itemView.findViewById(R.id.TitleName);
            keyWords = (TextView) itemView.findViewById(R.id.Bkey);
            description = (TextView) itemView.findViewById(R.id.Bdiception);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.blitme:
                    FindLast last = list.get(position);
                    if (itemLisenter != null){
                        itemLisenter.itemClickLisenter(last);
                    }

                break;
                default:
                    break;

            }
        }
    }
}
